package com.freesky.provider.content.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freesky.common.dao.entity.Item;
import com.freesky.common.dao.entity.Panel;
import com.freesky.common.dao.entity.PanelContent;
import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.exception.EMallException;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.pojo.PanelCategoryId;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.TimeUtil;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.provider.content.service.ICommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonServiceImpl implements ICommonService {
    @Autowired
    private IRedisTemplate redisTemplate;

    @Override
    public JSONResponse add(PanelContent panelContent) {

        panelContent.setCreateTime(TimeUtil.getDateTimeNow());
        panelContent.setUpdateTime(TimeUtil.getDateTimeNow());
        boolean insert = panelContent.insert();
        if (!insert) {
            throw new EMallException("添加首页板块内容失败");
        }
        //同步导航栏缓存
        if (panelContent.getPanelId().equals(PanelCategoryId.NAV) ) {
            updateNavListRedis();
        }
        //同步缓存
        deleteHomeRedis();
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    /**
     * panelId=0,查询导航栏列表
     *
     * @param panelId
     * @return
     */
    @Override
    public DataTablesResult getPanelContentListByPanelId(int panelId) {
        DataTablesResult result = new DataTablesResult();
        List<PanelContent> list = new PanelContent().selectList(new EntityWrapper<>().eq("panel_id", panelId));
        for (PanelContent content : list) {
            if (content.getItemId() != null) {
                Item item = new Item();
                item.setId(content.getItemId());
                item = item.selectById();
                if (item != null) {
                    content.setItemName(item.getTitle());
                    content.setSalePrice(item.getPrice());
                    content.setSubTitle(item.getSellPoint());
                }
            }
        }
        result.setData(list);
        return result;
    }

    @Override
    public JSONResponse delete(int[] ids) {
        if (ids == null) {
            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        }
        for (int id : ids) {
            PanelContent panelContent = new PanelContent();
            panelContent.setId(id);
            boolean delete = panelContent.deleteById();
            if (!delete) {
                throw new EMallException("删除首页板块失败");
            }
            //同步导航栏缓存
            if (id == PanelCategoryId.NAV) {
                updateNavListRedis();
            }
            //同步缓存
            deleteHomeRedis();
        }

        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse updateContent(PanelContent panelContent) {

        PanelContent old = new PanelContent();
        old.setId(panelContent.getId());
        old = old.selectById();

        if (StringUtils.isBlank(panelContent.getPicUrl())) {
            panelContent.setPicUrl(old.getPicUrl());
        }
        panelContent.setCreateTime(old.getCreateTime());
        panelContent.setUpdateTime(TimeUtil.getDateTimeNow());
        boolean update = panelContent.updateById();
        if (!update) {
            throw new EMallException("更新板块内容失败");
        }
        //同步导航栏缓存
        if (panelContent.getPanelId() == PanelCategoryId.NAV) {
            updateNavListRedis();
        }
        //同步缓存
        deleteHomeRedis();
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }


    public void updateNavListRedis() {
        redisTemplate.delete(RedisKeyEnum.HOME_NAV.getKey());
    }
    /**
     * 同步首页缓存
     */
    public void deleteHomeRedis() {
        redisTemplate.delete(RedisKeyEnum.HOME_PANEL_CONTENT.getKey());
    }

    @Override
    public List<Panel> getPanelAndContentsById(Integer panelId) {
        List<Panel> list = new Panel().selectList(new EntityWrapper().eq("id", panelId).eq("status", 1));
        if (list != null) {
            for (Panel panel : list) {
                List<PanelContent> contentList = new PanelContent().selectList(new EntityWrapper().eq("panel_id", panel.getId()).orderBy("sort_order", true));
                for (PanelContent content : contentList) {
                    if (content.getItemId() != null) {
                        Item item = new Item();
                        item.setId(content.getItemId());
                        item = item.selectById();
                        content.setItemName(item.getTitle());
                        content.setSalePrice(item.getPrice());
                        content.setSubTitle(item.getSellPoint());
                        content.setPicUrl(content.getPicUrl());
                    }
                }
                panel.setPanelContents(contentList);
            }
        }
        return list;
    }
}
